﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaleOnlineSystem.Admin.Controls
{
    public partial class ViewProduct : System.Web.UI.UserControl
    {
        public string image;
        public string name;
        public string discription;
        public string price;
        public string discount;
        public string summary;
        public string cate1_id;
        public string cate2_id;
        public string cate3_id;
        public string idcolor;
        public string idsize;
        public string idvendor;
        public string cate1_name;
        public string bigimage;
        public string smallimage;
        public int idproduct;
        public string namecate1;
        public string namecate2;
        public string namecate3;
        public string namevendor;
        public string namecolor;
        public string size;
        public string imagesmall1;
        public string imagesmall2;
        public string imagesmall3;
        protected void Page_Load(object sender, EventArgs e)
        {
           
                if (Request.QueryString["idproduct"] != null)
                {
                    idproduct = Convert.ToInt32(Request.QueryString["idproduct"]);
                    DataTable dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Product", "*", "Product_ID", idproduct, "selectwhereid");
                    cate1_id = dt.Rows[0]["Category_ID1"].ToString();
                    cate2_id = dt.Rows[0]["Category_ID2"].ToString();
                    cate3_id = dt.Rows[0]["Category_ID3"].ToString();
                    idcolor = dt.Rows[0]["Color_ID"].ToString();
                    idsize = dt.Rows[0]["Size_ID"].ToString();
                    idvendor = dt.Rows[0]["Vendor_ID"].ToString();
                    image = dt.Rows[0]["Product_Image"].ToString();
                    smallimage = dt.Rows[0]["Product_MultiImage"].ToString();
                    name = dt.Rows[0]["Product_Name"].ToString();
                    discription = dt.Rows[0]["Product_Descriptions"].ToString();
                    price = dt.Rows[0]["Product_SalesPrice"].ToString();
                    discount = dt.Rows[0]["Product_Discount"].ToString();
                    summary = dt.Rows[0]["Product_Summary"].ToString();

                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID1", cate1_id, "selectwhereid");
                    namecate1 = dt.Rows[0]["Category_Name"].ToString();
                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID1", cate2_id, "selectwhereid");
                    namecate2 = dt.Rows[0]["Category_Name"].ToString();
                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Category", "*", "Category_ID1", cate3_id, "selectwhereid");
                    namecate3 = dt.Rows[0]["Category_Name"].ToString();
                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Vendor", "*", "Vendor_ID", idvendor, "selectwhereid");
                    namevendor = dt.Rows[0]["Vendor_Name"].ToString();
                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Color", "*", "Color_ID", idcolor, "selectwhereid");
                    namecolor = dt.Rows[0]["Color_Name"].ToString();
                    dt = DAL.DCollector.DBCollector.Instance.StoredProcedureSelectWhereByID("tbl_Size", "*", "Size_ID", idsize, "selectwhereid");
                    size = dt.Rows[0]["Size_Name"].ToString();
                    string[] arraysmallimage = new string[3];
                    arraysmallimage[0] = arraysmallimage[1] = arraysmallimage[2] = "";

                    if (smallimage != "")
                    {

                        string[] splitimagesmall = smallimage.Split(',');
                        for (int i = 0; i < splitimagesmall.Length; i++)
                        {
                            arraysmallimage[i] = splitimagesmall[i].ToString();
                        }

                    }
                    imagesmall1 = arraysmallimage[0].ToString();
                    imagesmall2 = arraysmallimage[1].ToString();
                    imagesmall3 = arraysmallimage[2].ToString();
                }                       
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int productid = Convert.ToInt32(Request.QueryString["idproduct"]);
                DAL.DCollector.DBCollector.Instance.StoredProcedureDeleteByRows("tbl_Product", "Product_ID", productid.ToString());
                Response.Redirect("/Admin/Index.aspx?panel=1&sub=1");
            }
            catch (Exception)
            {
                Response.Redirect("/error.htm");
            }         
        }
    }
}